package com.rh.bmw4s.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rh.bmw4s.R;
import com.rh.bmw4s.bean.IndexGridBean;
import com.rh.bmw4s.comm.ConfigValue;
import com.rh.bmw4s.ui.BuyCarActivity;
import com.rh.bmw4s.ui.MainActivity;
import com.rh.bmw4s.ui.WantToBuyActivity;
import com.rh.bmw4s.ui.WebShowActivity;
import com.rh.bmw4s.utills.SPUtils;
import com.rh.bmw4s.utills.ToastUtils;
import com.rh.bmw4s.utills.glide.GlideImageLoader;


/**
 * 首页adapter
 */

public class IndexAdapter extends ListBaseAdapter<IndexGridBean.DatasBean> {

    private GlideImageLoader imageLoader;
    private int anInt;

    public IndexAdapter(Context context, int anInt) {
        super(context);
        this.anInt = anInt;
        imageLoader = new GlideImageLoader(context);
    }

    public IndexAdapter(Context context) {
        super(context);
        imageLoader = new GlideImageLoader(context);
    }

    @Override
    public int getLayoutId() {
        return R.layout.indexgrid_item;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, final int position) {
        final IndexGridBean.DatasBean bean = mDataList.get(position);

        LinearLayout linearLayout = holder.getView(R.id.card_view);
        ImageView imageView = holder.getView(R.id.index_grid_img);
        TextView textView = holder.getView(R.id.index_grid_text);

        imageLoader.display(imageView, bean.getLogo());
        textView.setText(bean.getCategoryname());
        //0 1 3 4
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MainActivity mainActivity = (MainActivity) mContext;
                if (position == 0) {
                    mainActivity.setChoose(2);
                    return;
                }
                if (position == 1) {
                    mainActivity.setChoose(3);
                    return;
                }
                if (position == 3) { //汽车用品
                    Intent i = new Intent(mContext, BuyCarActivity.class);
                    i.putExtra("id", bean.getId());
                    i.putExtra("name", bean.getCategoryname());
                    i.putExtra("exters", 3);
                    i.putExtra("mysearchtype", 4);  //用的同一个界面但是跳转到搜索时不同
                    mContext.startActivity(i);
                    return;
                }
                if (position == 4) {  //轮胎
                    Intent i = new Intent(mContext, BuyCarActivity.class);
                    i.putExtra("id", bean.getId());
                    i.putExtra("name", bean.getCategoryname());
                    i.putExtra("exters", 4);
                    i.putExtra("mysearchtype", 5);
                    mContext.startActivity(i);
                    return;
                }

                if (position == 6) {
                    SPUtils.getInstance().put(ConfigValue.isOpenEvents, true);
                }
                Intent intent = new Intent(mContext, WebShowActivity.class);
                intent.putExtra("url", bean.getShow() + "/mem_id/" + anInt);
                mContext.startActivity(intent);

            }
        });

    }

}
